package com.example.hotHub.service.impl;


import com.example.hotHub.model.NormalHotSearch;
import com.example.hotHub.service.TencentHotPointService;
import com.example.hotHub.utils.RestTemplateUtil;
import com.example.hotHub.utils.SimpleMap;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TencentHotPointServiceImpl implements TencentHotPointService {

    @SneakyThrows
    @Override
    public List<NormalHotSearch> getHotPointList() {
        RestTemplateUtil restTemplateUtil = new RestTemplateUtil();
        SimpleMap response = restTemplateUtil.doGet("https://i.news.qq.com/trpc.qqnews_web.kv_srv.kv_srv_http_proxy/list?sub_srv_id=24hours&srv_id=pc&offset=0&limit=199&strategy=1&ext={\"pool\":[\"top\"],\"is_filter\":7,\"check_type\":true}", null, null, SimpleMap.class);

        List<SimpleMap> list = response.getMap("data")
                .getList("list", SimpleMap.class);

        List<NormalHotSearch> normalHotSearchList = new ArrayList<>();
        for (SimpleMap element : list) {
            String title = element.getString("title");
            String url = element.getString("url");
            String imageUrl = element.getString("img");
            Integer hotValue = Integer.parseInt(String.valueOf(element.get("comment_num")));

            NormalHotSearch normalHotSearch = new NormalHotSearch()
                    .setTitle(title)
                    .setUrl(url)
                    .setDescribe(title)
                    .setHotValue(hotValue)
                    .setImageUrl(imageUrl);

            normalHotSearchList.add(normalHotSearch);
        }

        normalHotSearchList = normalHotSearchList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

        log.debug("成功爬取腾讯热点: {}", normalHotSearchList);

        return normalHotSearchList;
    }
}
